package com.OnTimeTeam.OnTime;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FilesContentAdapter extends ArrayAdapter<File> {

	private ArrayList<File> files; //all alarms details
	private ArrayList<Boolean> playerArray;
	private ArrayList<Integer> durationsArray;
	///////////////
	//CTOR - init' the array of AlarmFeatures and ApplicationContext object
	public FilesContentAdapter(Context context, int textViewResourceId, ArrayList<File> files, ArrayList<Boolean> playerArray, ArrayList<Integer> durationsArray) {
		super(context, textViewResourceId, files);
		this.files = files;
		this.playerArray = playerArray;
		this.durationsArray = durationsArray;
	}
	
	public void setItems(ArrayList<File> files, ArrayList<Boolean> playerArray, ArrayList<Integer> durationsArray) {
		this.files = files;
		this.playerArray = playerArray;
		this.durationsArray = durationsArray;
		
		notifyDataSetChanged();
	}

	////////////////////
	//This method sets the view of ONE item in the list (it gets it's index - it's the same index as the AlarmFeatures ArrayList)
	public View getView(final int index, View convertView, ViewGroup parent) {
	
		View view = convertView;
		LayoutInflater inflater = null;
		
		if (view == null) {
			inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.file_line, null);//set the view
		}
		
		TextView duration = (TextView)view.findViewById(R.id.duration);
		TextView currentFileName = (TextView)view.findViewById(R.id.choiceItem);
		ImageView currentImage = (ImageView)view.findViewById(R.id.itemImage); 
		currentImage.setFocusable(false);
		currentImage.setClickable(false);
		
		File file = files.get(index);
		
		//set the name of the file
		currentFileName.setText(file.getName());
		
		if(files.get(index).isDirectory())
			currentImage.setImageResource(R.drawable.folder);
		else  {
			
			int durationInteger = durationsArray.get(index);
			
			long secs = TimeUnit.MILLISECONDS.toSeconds(durationInteger) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(durationInteger));
			long mins = TimeUnit.MILLISECONDS.toMinutes(durationInteger);
			String minutesString, secondsString;
			
			if(mins < 10)
				minutesString = "0" + Long.toString(mins);
			else
				minutesString = Long.toString(mins);

			if(secs < 10)
				secondsString = "0" + Long.toString(secs);
			else
				secondsString = Long.toString(secs);			
			
			duration.setText(minutesString + ":" + secondsString);
			
			
			if(!playerArray.get(index))
				currentImage.setImageResource(R.drawable.play_button);
			else
				currentImage.setImageResource(R.drawable.stop_button);
		}
		
        Animation animation = null; 
        
        //animation = new TranslateAnimation(metrics.widthPixels/2, 0, metrics.widthPixels/2, 0); 
        animation = new ScaleAnimation((float)1.0, (float)1.0 ,(float)0, (float)1.0);
        animation.setDuration(200);  
        view.startAnimation(animation); 
        animation = null; 
		
		//return the view of the item
		return view;
	
	}//end of method 'getView'
	
}//EOF

